// Copyright (C) 1998-2008, Sumisho Computer Systems Corp. All Rights Reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @author Hitoshi Okada
 */

package com.curlap.orb.context;

import java.lang.reflect.InvocationTargetException;
import javax.servlet.ServletContext;

import org.apache.commons.logging.LogFactory;

import com.curlap.orb.servlet.CurlConfigFactory;

/**
 * ApplicationContextFactory
 */
public class ApplicationContextFactory 
{
    private static ApplicationContextFactory instance = null;
    private AbstractApplicationContext context = null;

    protected ApplicationContextFactory(ServletContext context) throws ApplicationContextException
    {
        Class applicationContextClass = 
            (CurlConfigFactory.getInstance(context)).getCurlConfig().getApplicationContextClass();
        if (applicationContextClass != null)
        {
            try {
                this.context = 
                    (AbstractApplicationContext)(applicationContextClass.getDeclaredConstructor(ServletContext.class).newInstance(context));
            } 
            catch (NoSuchMethodException e) 
            {
                throw new ApplicationContextException(e);
            } 
            catch (IllegalAccessException e) 
            {
                throw new ApplicationContextException(e);
            } 
            catch (InvocationTargetException e) 
            {
                throw new ApplicationContextException(e);
            }
            catch (InstantiationException e)
            {
                throw new ApplicationContextException(e);
            }
        }
        else
        {
            // default
            this.context = new ServletApplicationContext(context);
        }
    }

    // new instance
    public static ApplicationContextFactory getInstance(ServletContext context)
    {
        if (ApplicationContextFactory.instance == null) 
        {
            try 
            {
                ApplicationContextFactory.instance = new ApplicationContextFactory(context);
            }
            catch (ApplicationContextException e) 
            {
                // fatal log
                (LogFactory.getLog(ApplicationContextFactory.class)).fatal(e.getMessage(), e);                
            }
        }
        return ApplicationContextFactory.instance;
    }

    // get ApplicationContext
    public AbstractApplicationContext getApplicationContext()
    {
        return context;
    }
}
